﻿using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.EntityFrameworkCore;

namespace Jack.EntityFrameworkCore
{
    public static class PagingExtensions
    {
        public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> query, int page, int pageSize)
        {
            return query.Skip((page - 1) * pageSize).Take(pageSize);
        }

        public static async Task<(int Count, List<T> Items)> CountAndPagingAsync<T>(this IQueryable<T> source, int page, int pageSize, CancellationToken token = default)
        {
            return (await source.CountAsync(token), await source.ApplyPaging(page, pageSize).ToListAsync(token));
        }
    }
}
